package com.wangijun.yuan.admin.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.admin.base.CommonPage;
import com.wangijun.yuan.admin.base.CommonResult;
import com.wangijun.yuan.admin.system.domain.RoleMenu;
import com.wangijun.yuan.admin.system.domain.SysRole;
import com.wangijun.yuan.admin.system.model.dto.RoleQryParam;
import com.wangijun.yuan.admin.system.model.dto.RoleSaveParam;
import com.wangijun.yuan.admin.system.model.dto.RoleUpdateParam;
import com.wangijun.yuan.admin.system.model.vo.RoleVO;
import com.wangijun.yuan.core.model.IdParam;
import com.wangijun.yuan.core.model.IdsParam;
import com.wangijun.yuan.admin.system.service.SysMenuService;
import com.wangijun.yuan.admin.system.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import java.util.List;
@Tag(name = "角色相关接口")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private SysMenuService menuService;
    @Resource
    private SysRoleService roleService;

    @Operation(summary = "查", description = "查")
    @PostMapping("/list")
    CommonResult<CommonPage<RoleVO>> list(@RequestBody RoleQryParam param) {
        return CommonResult.data(roleService.getList(param));
    }
    @Operation(summary = "增", description = "增")
    @PostMapping("/add")
    CommonResult<Boolean> add(@RequestBody @Validated RoleSaveParam param) {
        SysRole sysRole = BeanUtil.copyProperties(param, SysRole.class);
        sysRole.setId(IdWorker.getId());
        for (Long menuId : param.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(sysRole.getId());
            roleMenu.setMenuId(menuId);
            Db.save(roleMenu);
        }
        Db.save(sysRole);
        return CommonResult.data(true);
    }

    @Schema(description = "菜单数载体")
    public record MenuIdVO(
            @Schema(description = "菜单树")
            List<Tree<Long>> menuTree,
            @Schema(description = "全部菜单ID")
            List<Long> allMenuIds
    ) {
    }

    @Operation(summary = "获取菜单树", description = "获取菜单树")
    @GetMapping("/menuTree")
    CommonResult<MenuIdVO> menuTree() {
        return CommonResult.data(menuService.menuTree());
    }

    @Operation(summary = "改", description = "改")
    @PostMapping("/update")
    CommonResult<Boolean> update(@RequestBody @Validated RoleUpdateParam role) {
        roleService.update(role);
        return CommonResult.data(true);
    }
    @Operation(summary = "删", description = "删")
    @PostMapping("/del")
    CommonResult<Boolean> del(@RequestBody IdParam param) {
        Db.lambdaUpdate(RoleMenu.class)
                .eq(RoleMenu::getRoleId, param.getId())
                .remove();
        roleService.del(param.getId());
        return CommonResult.data(true);
    }
    @Operation(summary = "删", description = "删")
    @PostMapping("/delBatch")
    CommonResult<Boolean> delBatch(@RequestBody IdsParam param) {
        Db.lambdaUpdate(RoleMenu.class)
                .in(RoleMenu::getRoleId, param.getIds())
                .remove();
        roleService.delBatch(param.getIds());
        return CommonResult.data(true);
    }

    @Operation(summary = "获取菜单ID", description = "获取菜单ID")
    @PostMapping("/getMenuIdById")
    CommonResult<List<Long>> getMenuIdById(@RequestBody IdParam param) {
        return CommonResult.data(roleService.getMenuIdById(param.getId()));
    }

}
